package com.wx.mythread.threadmethod;


/**
 * 两个线程交替打印0-100
 * 使用synchronize
 */
public class StudyPrintSyn{

    private static int count;
    private static volatile Object lock = new Object();

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while ( count < 100 ){
                    synchronized (lock){
                        if ((count & 1) == 0){
                            System.out.println("偶数线程："+count);
                            count++;
                        }
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while ( count < 100 ){
                    synchronized (lock){
                        if ((count & 1) == 1){
                            System.out.println("奇数线程："+count);
                            count++;
                        }
                    }
                }
            }
        }).start();
    }


}
